package com.wtb.javatool.action;

import com.fy.wetoband.tool.Tool;
import com.fy.wetoband.utils.RequestUtil;
import com.sun.xml.bind.v2.TODO;
import com.wtb.javatool.common.Result;
import com.wtb.javatool.entity.Dish;
import com.wtb.javatool.entity.Project;
import com.wtb.javatool.entity.Tag;
import com.wtb.javatool.entity.User;
import com.wtb.javatool.service.IDishService;
import com.wtb.javatool.service.IProjectService;
import com.wtb.javatool.service.UserService;
import com.wtb.javatool.vo.DishDto;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;


import static org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_PROTOTYPE;

@Api(value = "javatool", description = "java工具示例")
@Scope(SCOPE_PROTOTYPE) //务必使用多实例模式
@ComponentScan("com.wtb.javatool")
@Component
public class JavaToolDemoAction extends Tool {

    @Autowired
    private IProjectService projectService;

    @Autowired
    private UserService userService;
    /**
     * 根据学生id查询项目
     *
     *
     */
    @Action
    public Result<ArrayList<Project>> SelectProjectByStudentId(HttpServletRequest request, HttpServletResponse response) {
       int studentId = Integer.parseInt(request.getParameter("studentId"));
        ArrayList<Project> projects = projectService.SelectProjectByStudentId(studentId);
        return Result.success(projects);
    }
    /**
     * 查询公开项目
     *
     *
     */
    @Action
    public Result<ArrayList<Project>> SelectProjectByIsPublic(HttpServletRequest request, HttpServletResponse response) {
        int isPublic = Integer.parseInt(request.getParameter("isPublic"));
        ArrayList<Project> projects = projectService.SelectProjectByIsPublic(isPublic);
        return Result.success(projects);
    }
    /**
     *
     *查询项目详细信息
     *
     */
    @Action
    public Result<Project> SelectProjectDetailsByProjectId(HttpServletRequest request, HttpServletResponse response) {
        int projectId = Integer.parseInt(request.getParameter("projectId"));
        Project project = projectService.SelectProjectDetailsByProjectId(projectId);
        return Result.success(project);
    }
    /**
     *
     *根据信息动态模糊搜索项目
     *
     */
    @Action
    public Result<ArrayList<Project>> SelectProjectByInfo(HttpServletRequest request, HttpServletResponse response) {
        String projectName = null;
        String studentName=null;
        int stars =0;
        String type = request.getParameter("type");
        String keyword = request.getParameter("keyword");
        switch (type) {
            case "projectName":
                projectName = keyword;
                break;
            case "studentName":
                studentName = keyword;
                break;
            case "stars":
                stars = Integer.parseInt(keyword);
                break;
            default:
                return Result.error("不支持的搜索类型");
        }
        ArrayList<Project> projects = projectService.SelectProjectByInfo(projectName, studentName, stars);
        return Result.success(projects);
    }
    @Action
    public Result<ArrayList<Project>> SelectStudentProjectByInfo(HttpServletRequest request, HttpServletResponse response) {
        String projectName = null;
        String studentName=null;
        int stars =0;
        int studentId = Integer.parseInt(request.getParameter("studentId"));
        String type = request.getParameter("type");
        String keyword = request.getParameter("keyword");
        switch (type) {
            case "projectName":
                projectName = keyword;
                break;
            case "studentName":
                studentName = keyword;
                break;
            case "stars":
                stars = Integer.parseInt(keyword);
                break;
            default:
                return Result.error("不支持的搜索类型");
        }
        ArrayList<Project> projects = projectService.SelectStudentProjectByInfo(studentId,projectName, studentName, stars);
        return Result.success(projects);
    }

    /**
     *
     *查询全部user
     *
     */
    @Action
    public Result<ArrayList<User>> SelectAllUser(HttpServletRequest request, HttpServletResponse response) {
        ArrayList<User> Users = userService.SelectAllUser();
        return Result.success(Users);
    }
    /**
     *
     *查询全部tag
     *
     */
    @Action
    public Result<ArrayList<Tag>> SelectAllTag(HttpServletRequest request, HttpServletResponse response) {
        ArrayList<Tag> Tags = projectService.SelectAllTag();
        return Result.success(Tags);
    }
}